using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Urbania.Components
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class EventComponent : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private GUI.GUIContainer container = new GUI.GUIContainer();
        private string script;
        SpriteBatch spriteBatch;

        public EventComponent(Game game, string script)
            : base(game)
        {
            this.script = script;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
            Program.lua.Register("CreatePopUp", this, this.GetType().GetMethod("CreatePopUp"));
            Program.lua.Register("CreateText", this, this.GetType().GetMethod("CreateText"));
            container.AddFrame(new GUI.Elements.Frame(ref Program.lua));

            Program.lua.LuaVirtual.DoFile(Utilities.Loader.GetScriptFile(script));
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            base.LoadContent();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            container.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            spriteBatch.Begin();
            container.Draw(gameTime, spriteBatch);
            spriteBatch.End();
        }

        public GUI.Elements.PopUp CreatePopUp(int x, int y, int width, int height)
        {
            GUI.Elements.PopUp p = new GUI.Elements.PopUp(new Rectangle(x, y, width, height), Color.Gray, ref
                Program.lua, container.GetBaseFrame());
            
            container.GetBaseFrame().AddPopUp(p);
            return p;
        }

        public GUI.Elements.TextElement CreateText(string text, int x, int y)
        {
            GUI.Elements.TextElement t = new GUI.Elements.TextElement(new Vector2(x, y),
                text, text, Color.Black, container.GetBaseFrame());
            return t;
        }
    }
}
